import { LoaderMgr } from "../framework/LoaderMgr";
import { gen_handler } from "../framework/Utils";
import { ServerConfig } from "../config/ServerConfig";
import Tool from "../utils/Tool";

const assetList: string [] = [
    "common_player_data",
    "common_player_data_level_get",
    "common_formation_data",
    "common_gift_data",
    "common_rank_award",
    "common_tips"
]

export default class DataMgr {
    private static m_instance: DataMgr
    public inited: boolean = false;
    private tables:any = null;
    private constructor () {}
  
    static get Instance()
    {
        if (!DataMgr.m_instance) {
            DataMgr.m_instance = new DataMgr();
        }
        return DataMgr.m_instance
    }

    init () {
        this.load();
    }

    load() {        
        // console.log(`load_external_img ${img_url}`);
        LoaderMgr.Instance.loadStaticAssets(ServerConfig.STATIC_URL + "data/",".json", assetList, gen_handler((res) => {
            this.tables = res;
            this.inited = true;
        }), "json");
    }

    getTable(tableName) {
        if (this.tables) {
            return this.tables[tableName];  
        }else {
            return {};
        }
    }

    // 获取所有球员的信息
    getAllPlayerData () {
        return this.getTable("common_player_data_level_get");
    }

    // 获取一个球员的信息
    getOnePlayerData (player_id, level) {
        const table = this.getTable("common_player_data");
        if (table == null) {
            console.warn("Not find data: common_player_data");
        }

        if (table[player_id] == null) {
            console.warn(player_id + " not find data in common_player_data");
            return null;
        }

        if (table[player_id][level] == null) {
            console.warn(player_id + " not find "+ level + " level in common_player_data");
            return null;
        }

        return table[player_id][level]
    }

    // 获取一个球员的信息,只有id，没有等级，需要遍历 level_get == 1
    getOnePlayerInitData(player_id) {
        const table = this.getAllPlayerData();
        if (table == null) {
            console.warn("Not find data: common_player_data");
        }

        if (table[player_id] == null) {
            console.warn(player_id + " not find data in common_player_data");
            return null;
        }

        return table[player_id]
    }

    // 获取所有阵容的信息
    getAllFormationData () {
        return this.getTable("common_formation_data");
    }

    // 获取一个球员的信息
    getOneFormationData (formation_id) {
        const table = this.getAllFormationData();
        if (table == null) {
            console.warn("Not find data: common_formation_data");
        }

        if (table[formation_id] == null) {
            console.warn(formation_id + " not find data in common_formation_data");
            return null;
        }

        return table[formation_id]
    }

    // 获得一个卡包信息
    getOneGiftData (id) {
        if (id <= 0) {
            return null;
        }
        
        const table = this.getTable("common_gift_data");
        if (table == null) {
            console.warn("Not find data: common_gift_data");
        }

        if (table[id] == null) {
            console.warn(id + " not find data in common_gift_data");
            return null;
        }

        return table[id]
    }

    // 获取所有阵容的信息
    getAllRankAwardData () {
        return this.getTable("common_rank_award");
    }

    // 获取所有阵容的信息
    getOneRankAwardData (level) {
        if (level <= 0) {
            return null;
        }
        
        const table = this.getAllRankAwardData();
        if (table == null) {
            console.warn("Not find data: common_rank_award");
        }

        if (table[level] == null) {
            console.warn(level + " not find data in common_rank_award");
            return null;
        }

        return table[level]
    }

    getOneTip () {
        const table:any [] = this.getTable("common_tips");
        if (table == null) {
            console.warn("Not find data: common_tips");
        }
        const num = Tool.getRandomIntNum(0, table.length - 1);
        return table[num]
    }
}
